package com.example.serviceprovider.controller;

import com.example.serviceprovider.actuator.HealthStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author:jiangqikun
 * @Date:2022/3/11 18:06
 **/

@RestController
public class ProviderController {

    @Autowired
    private HealthStatusService healthStatusSrv;

    private AtomicInteger count=new AtomicInteger();

    @Value("${server.port}")
    private String port;

    @GetMapping("/send-message")
    public String sendMessage(){
        return "port: "+port;
    }

    @GetMapping("test-hystrix")
    public String testHystrix(){
        int i=1/0;
        return "port: "+port;
    }

    /**
     * 根据服务状态：控制服务上下线
     * localhost:端口/health?status=true    上线
     * localhost:端口/health?status=false   下线
     */
    @GetMapping("/health")
    public String health(@RequestParam("status") Boolean status) {
        healthStatusSrv.setStatus(status);
        return healthStatusSrv.getStatus();
    }

    /**
     * 测试超时
     */
    @GetMapping("test-time")
    public String testTime(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int  i=count.incrementAndGet();
        System.out.println(port+"第"+ i + "次调用");
        return "port: "+port;
    }

}
